package com.qf.openapi.web.master.service.impl;

import cn.hutool.crypto.digest.DigestUtil;
import com.qf.openapi.web.master.dao.UserDao;
import com.qf.openapi.web.master.entity.User;
import com.qf.openapi.web.master.service.UserService;
import com.qf.openapi.web.master.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public User getUser(Integer id) {
        return userDao.getUserByID(id);
    }

    @Override
    public User getUserByName(String name) {
        return userDao.getUserByName(name);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateUserStatus(User user) {
        userDao.updateUser(user);
    }

    @Override
    public User login(UserVo userVo) {

        User userByName = userDao.getUserByName(userVo.getUsername());

        if (userByName!=null){
            String s = DigestUtil.md5Hex(userVo.getPassword());
            if (s.equals(userByName.getPassword())){
                return userByName;
            }
        }
        return null;
    }
}
